#include "../include/client.h"

int delSpace(char *temp,char *buf){
    size_t idx = 0;
    for(size_t i = 0;i < strlen(temp);i++){
        if((temp[i]>='a'&&temp[i]<='z')||(temp[i]>='A'&&temp[i]<='Z')||(temp[i]>='0'&&temp[i]<='9')||(temp[i]=='.')||(temp[i]=='/')){
            idx = i;
            break;
        }
    }
    for(size_t i = 0;idx < strlen(temp);i++,idx++){
        if(temp[idx] == ' '){
            continue;
        }
        buf[i] = temp[idx];
    }
    return 0;
}

int splitCommand(const char *cmd,char *buf){
    size_t idx = 0;
    for(size_t i = 0;i < strlen(cmd);i++){
        if(cmd[i]>='A'&&cmd[i]<='Z'){
            idx = i;
            break;
        }
    }
    char temp[30] = {0};
    for(size_t i = 0;idx < strlen(cmd);i++,idx++){
        temp[i] = cmd[idx];
    }
    size_t cmdSize = strlen(temp);
    switch(temp[0]){
    case 'C':
        if(temp[1] == 'D'){
            if(temp[2] == '\0'){
                printf("命令错误!\n");
                return -1;
            }
            memcpy(buf,temp + 2,cmdSize - 2);
            return CD;
        }
        printf("命令错误!\n");
        return -1;
    case 'L':
        if(temp[1] == 'S'){
            memcpy(buf,temp + 2,cmdSize - 2);
            return LS;
        }
        printf("命令错误!\n");
        return -1;
    case 'P':
        if(temp[1] == 'W'){
            if(temp[2] == 'D'){
                return PWD;
            }
            printf("命令错误!\n");
            return -1;
        }else if(temp[1] == 'U'){
            if(temp[2] == 'T'){
                if(temp[3] == 'S'){
                    if(temp[4] == '\0'){
                        printf("命令错误!\n");
                        return -1;
                    }
                    memcpy(buf,temp + 4,cmdSize - 4);
                    return PUTS;
                }
                printf("命令错误!\n");
                return -1;
            }
            printf("命令错误!\n");
            return -1;
        }
        printf("命令错误!\n");
        return -1;
    case 'G':
        if(temp[1] == 'E'){
            if(temp[2] == 'T'){
                if(temp[3] == 'S'){
                    if(temp[4] == '\0'){
                        printf("命令错误!\n");
                        return -1;
                    }
                    memcpy(buf,temp + 4,cmdSize - 4);
                    return GETS;
                }
                printf("命令错误!\n");
                return -1;
            }
            printf("命令错误!\n");
            return -1;
        }
        printf("命令错误!\n");
        return -1;
    case 'M':
        if(temp[1] == 'K'){
            if(temp[2] == 'D'){
                if(temp[3] == 'I'){
                    if(temp[4] == 'R'){
                        if(temp[5] == '\0'){
                            printf("命令错误!\n");
                            return -1;
                        }
                        memcpy(buf,temp + 5,cmdSize - 5);
                        return MKDIR;
                    }
                    printf("命令错误!\n");
                    return -1;
                }
                printf("命令错误!\n");
                return -1;
            }
            printf("命令错误!\n");
            return -1;
        }
        printf("命令错误!\n");
        return -1;
    case 'R':
        if(temp[1] == 'M'){
            if(temp[2] == 'D'){
                if(temp[3] == 'I'){
                    if(temp[4] == 'R'){
                        if(temp[5] == '\0'){
                            printf("命令错误!\n");
                            return -1;
                        }
                        memcpy(buf,temp + 5,cmdSize - 5);
                        return RMDIR;
                    }
                    printf("命令错误!\n");
                    return -1;
                }
                printf("命令错误!\n");
                return -1;
            }
            printf("命令错误!\n");
            return -1;
        }else if(temp[1] == 'E'){
            if(temp[2] == 'M'){
                if(temp[3] == 'O'){
                    if(temp[4] == 'V'){
                        if(temp[5] == 'E'){
                            if(temp[6] == '\0'){
                                printf("命令错误!\n");
                                return -1;
                            }
                            memcpy(buf,temp + 6,cmdSize - 6);
                            return REMOVE;
                        }
                        printf("命令错误!\n");
                        return -1;
                    }
                    printf("命令错误!\n");
                    return -1;
                }
                printf("命令错误!\n");
                return -1;
            }
            printf("命令错误!\n");
            return -1;
        }
        printf("命令错误!\n");
        return -1;
    default:
        printf("命令错误!\n");
        return -1;
    }
}



//int main(){
//    char buf1[1000] = {0};
//    char buf2[1000] = {0};
//    int ret = spilCommand("MKDIR  file  ",buf1);
//    printf("%d %s\n",ret,buf1);
//    delSpace(buf1,buf2);
//    printf("|%s|\n",buf2);
//    return 0;
//}    
